package app.android.com.wuandroidapp.test.view.definedview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import app.android.com.wuandroidapp.R;

/**
 * Created by Administrator on 2017/7/31.
 */

public class TitleBar extends RelativeLayout {
    public TitleBar(Context context) {
        super(context);
    }


    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
        String title = ta.getString(R.styleable.TitleBar_title);
        float titleTextSize = ta.getDimension(R.styleable.TitleBar_titleTextSize, 10);
        int titleColor = ta.getColor(R.styleable.TitleBar_titleColor, 0);

        int leftTextColor = ta.getColor(R.styleable.TitleBar_leftTextColor, 0);
        Drawable leftBackground = ta.getDrawable(R.styleable.TitleBar_leftBackground);
        String leftText = ta.getString(R.styleable.TitleBar_leftText);
        float lefttextSize = ta.getDimension(R.styleable.TitleBar_leftTextSize, 10);

        int rightTextColor = ta.getColor(R.styleable.TitleBar_rightTextColor, 0);
        Drawable rightBackground = ta.getDrawable(R.styleable.TitleBar_rightBackground);
        String rightText = ta.getString(R.styleable.TitleBar_rightText);
        float rightTextSize = ta.getDimension(R.styleable.TitleBar_rightTextSize, 10);
        ta.recycle();

        mLeftButton=new Button(context);
        mRightButton=new Button(context);
        mTitleView=new TextView(context);


        mTitleView.setText(title);
        mTitleView.setTextSize(titleTextSize);
        mTitleView.setGravity(Gravity.CENTER);
        mTitleView.setTextColor(titleColor);

        mLeftButton.setText(leftText);
        mLeftButton.setBackground(leftBackground);
        mLeftButton.setTextColor(leftTextColor);
        mLeftButton.setTextSize(lefttextSize);



        mRightButton.setText(rightText);
        mRightButton.setBackground(rightBackground);
        mRightButton.setTextColor(rightTextColor);
        mRightButton.setTextSize(rightTextSize);

        LayoutParams mleftparams= (LayoutParams) new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        addView(mLeftButton,mleftparams);

        LayoutParams mRightparams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        mRightparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(mRightButton,mRightparams);

        LayoutParams mTitleParams=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT);
        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(mTitleView,mTitleParams);
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.rightClick();
            }
        });

        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.leftClick();
            }
        });
    }

    Button mLeftButton,mRightButton;
    TextView mTitleView;
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }


    public void setOntitlebarOnClickListener(titlebarClickListener listener){
        this.mListener=listener;
    }

    titlebarClickListener mListener;
    public interface titlebarClickListener{
        void leftClick();
        void  rightClick();
    }
}
